package com.kebabeaters.nakebab.client.ui.view;

import java.util.List;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.kebabeaters.nakebab.client.presenter.RestaurantDetailsPresenter;
import com.kebabeaters.nakebab.client.resources.ResourcesFactory;
import com.kebabeaters.nakebab.client.resources.client.KebabResources;
import com.kebabeaters.nakebab.client.ui.widget.TasterReview;
import com.kebabeaters.nakebab.client.ui.widget.UserReviewWidget;
import com.kebabeaters.nakebab.shared.dto.structures.Mark;
import com.kebabeaters.nakebab.shared.dto.structures.Review;

public class RestaurantDetailsView extends ViewImpl implements
		RestaurantDetailsPresenter.MyView {

	private final Widget widget;
	KebabResources resources = ResourcesFactory.getKebabResources();

	public interface Binder extends UiBinder<Widget, RestaurantDetailsView> {
	}

	@UiField(provided = true)
	protected Image image;
	@UiField
	protected Label header;
	@UiField
	protected Label name;
	@UiField
	protected Label address;
	@UiField
	protected Label price;
	@UiField
	protected Label sauce;
	@UiField
	protected Label hours;
	@UiField
	protected Label additional;
	@UiField
	protected TasterReview rev1;
	@UiField
	protected TasterReview rev2;
	@UiField
	protected FlowPanel userReviewsContainer;

	@Inject
	public RestaurantDetailsView(final Binder binder) {
		image = new Image(resources.defaultRestaurant().getSafeUri());
		image.setHeight("350px");
		image.setWidth("350px");

		widget = binder.createAndBindUi(this);

		header.setText("Kebabiarnia");
	}

	@Override
	public Widget asWidget() {
		return widget;
	}

	@Override
	public void setName(String name) {
		this.name.setText(name);
	}

	@Override
	public void setAddress(String adress) {
		this.address.setText(adress);
	}

	@Override
	public void setPrice(String price) {
		this.price.setText(price);
	}

	@Override
	public void setSauce(String sauce) {
		this.sauce.setText(sauce);
	}

	@Override
	public void setHours(String shift) {
		this.hours.setText(shift);
	}

	@Override
	public void setAdditional(String add) {
		this.additional.setText(add);
	}

	public void setRev1(Mark mark) {
		rev1.setBread(Integer.toString(mark.getBread()));
		rev1.setMeat(Integer.toString(mark.getMeat()));
		rev1.setNick(mark.getTaster().getName());
		rev1.setPrice(Integer.toString(mark.getPriceForSize()));
		rev1.setRec("Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum"
				+ "Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum");
		rev1.setSauce(Integer.toString(mark.getSauce()));
		rev1.setVegetables(Integer.toString(mark.getVegetables()));
		rev1.setPlus(mark.getPlusCount());
		rev1.setMinus(mark.getMinusCount());
	}

	public void setRev2(Mark mark) {
		rev2.setBread(Integer.toString(mark.getBread()));
		rev2.setMeat(Integer.toString(mark.getMeat()));
		rev2.setNick(mark.getTaster().getName());
		rev2.setPrice(Integer.toString(mark.getPriceForSize()));
		rev2.setRec("Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum"
				+ "Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum");
		rev2.setSauce(Integer.toString(mark.getSauce()));
		rev2.setVegetables(Integer.toString(mark.getVegetables()));
		rev2.setPlus(mark.getPlusCount());
		rev2.setMinus(mark.getMinusCount());
	}

	@Override
	public void setUserReviews(List<Review> reviews) {
		int i = 0;
		for (Review r : reviews) {
			UserReviewWidget urw = new UserReviewWidget();
			urw.setMarks(r.getMeatMark(), r.getBreadMark(),
					r.getVegetablesMark(), r.getPriceMark(), r.getSauceMark());
			urw.setDate(r.getDate());
			urw.setName(r.getUser());
			urw.setPlus(12 + i);
			urw.setMinus(16 - 3 * i);

			if (i % 2 == 0)
				urw.getElement().getStyle().setBackgroundColor("#648EE8");

			urw.setDescription(r.getDescription());

			if (r.getImageUrl() == null || r.getImageUrl().equals(""))
				urw.setImageUrl(ResourcesFactory.getKebabResources()
						.defaultAvatar().getSafeUri());
			else
				urw.setImageUrl(r.getImageUrl());

			userReviewsContainer.add(urw);
			i++;
		}
	}
}
